package com.smartcruise.task;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.smartcruise.R;
import com.smartcruise.data.cruiseData.CruiseContract;
import com.smartcruise.data.cruiseData.CruiseData;
import com.smartcruise.data.cruiseData.NodeTaskListBean;
import com.smartcruise.task.nodeTaskDataEdit.NodeTaskDataEditActivity;
import com.smartcruise.util.ListUtils;
import com.smartcruise.util.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.realm.OrderedRealmCollection;
import io.realm.RealmBaseAdapter;

/**
 * Created by FengChaoQun
 * on 2018/1/25 0025
 * 节点任务adapter
 */

public class NodeAdapter extends RealmBaseAdapter<NodeTaskListBean> {

    private CruiseData cruiseData;
    private Context context;

    public NodeAdapter(@Nullable OrderedRealmCollection<NodeTaskListBean> data, Context context, CruiseData cruiseData) {
        super(data);
        this.cruiseData = cruiseData;
        this.context = context;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_node_view, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final NodeTaskListBean nodeTaskListBean = getItem(position);
        viewHolder.nodeName.setText(nodeTaskListBean.getPatrollingNodeEntity().getName());

        if (nodeTaskListBean.isNeedText()) {
            viewHolder.book.setVisibility(View.VISIBLE);
            viewHolder.book.setSelected(nodeTaskListBean.isHaveText());
        } else {
            viewHolder.book.setVisibility(View.GONE);
        }

        if (nodeTaskListBean.isNeedPicture()) {
            viewHolder.camera.setVisibility(View.VISIBLE);
            viewHolder.camera.setSelected(ListUtils.isHaveContent(nodeTaskListBean.getShowImage(cruiseData)));
        } else {
            viewHolder.camera.setVisibility(View.GONE);
        }

        if (nodeTaskListBean.isNeedVideo()) {
            viewHolder.video.setVisibility(View.VISIBLE);
            viewHolder.video.setSelected(ListUtils.isHaveContent(nodeTaskListBean.getShowVideo(cruiseData)));
        } else {
            viewHolder.video.setVisibility(View.GONE);
        }

        if (nodeTaskListBean.isNeedAudio()) {
            viewHolder.audio.setVisibility(View.VISIBLE);
            viewHolder.audio.setSelected(ListUtils.isHaveContent(nodeTaskListBean.getShowAudio(cruiseData)));
        } else {
            viewHolder.audio.setVisibility(View.GONE);
        }

        switch (nodeTaskListBean.getUploadStatus()) {
            case 0:
                viewHolder.status.setText("编辑");
                break;
            case 1:
                viewHolder.status.setText("已上传概要数据");
                break;
            case 2:
                viewHolder.status.setText("已上传");
                break;
            default:
                viewHolder.status.setText("未知状态");
        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!CruiseContract.isStarted(cruiseData)) {
                    ToastUtils.showShort(context, "任务尚未开启,请先开启任务");
                    return;
                }
                if (cruiseData.isFromNet()) {
                    NodeTaskDataEditActivity.start(context, cruiseData.getId(), nodeTaskListBean.getNodeId());
                    return;
                }
//                if (CruiseContract.isOnPosition(LocationManager.getInstance().getLastData(), cruiseData, nodeTaskListBean)) {
                    NodeTaskDataEditActivity.start(context, cruiseData.getId(), nodeTaskListBean.getNodeId());
//                } else {
//                    ToastUtils.showShort(context, "当前不在指定位置范围内，请接近指定地点");
//                }
            }
        });

        return convertView;
    }


    static class ViewHolder {
        @BindView(R.id.nodeName)
        TextView nodeName;
        @BindView(R.id.book)
        ImageView book;
        @BindView(R.id.camera)
        ImageView camera;
        @BindView(R.id.video)
        ImageView video;
        @BindView(R.id.audio)
        ImageView audio;
        @BindView(R.id.status)
        TextView status;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
